//
//  AWNavigationController.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/4.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit

class AWNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            
            if viewControllers.count == 1 {
                
                let title = viewControllers.first?.title
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(backButtonDidClicked))
                
            } else {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backButtonDidClicked))
                
            }
            
            viewController.title = "当前显示的是\(viewControllers.count + 1)级控制器"
            
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func backButtonDidClicked() {
        
        self.popViewController(animated: true)
        
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
            
            return false
            
        }
        
        return true
        
    }
    
}
